﻿Imports SIGI.Common.BE
Imports System.Reflection
Imports System.ComponentModel

Module GenericMethods
    Public Sub GetCountries(pCmb As ComboBox)
        Dim wCountryController As CountryController = New CountryController
        Dim wCountry As CountryBE = New CountryBE
        Dim wCountries As List(Of CountryBE) = Nothing

        wCountries = wCountryController.Search()

        wCountry.Id = 0
        wCountry.Description = "Sin Selección"
        wCountries.Insert(0, wCountry)

        pCmb.ValueMember = "Id"
        pCmb.DisplayMember = "Description"
        pCmb.DataSource = wCountries
        pCmb.SelectedIndex = 0
    End Sub

    Public Sub GetProvinceByCountryId(pCountryId As Integer, pCmbProvince As ComboBox)
        Dim wProvince As ProvinceBE = New ProvinceBE
        Dim wProvinceList As List(Of ProvinceBE) = Nothing
        Dim wCountryController As CountryController = New CountryController

        wProvince.Id = 0
        wProvince.Description = "Sin Selección"

        wProvinceList = wCountryController.SearchProvinceByCountryId(pCountryId)
        wProvinceList.Insert(0, wProvince)
        pCmbProvince.ValueMember = "Id"
        pCmbProvince.DisplayMember = "Description"
        pCmbProvince.DataSource = wProvinceList
        pCmbProvince.SelectedIndex = 0
    End Sub

    Public Sub GetCityByProvinceId(pProvinceId As Integer, pCmbCity As ComboBox)
        Dim wCity As CityBE = New CityBE
        Dim wCityList As List(Of CityBE) = Nothing
        Dim wCountryController As CountryController = New CountryController

        wCity.Id = 0
        wCity.IdProvince = 0
        wCity.Description = "Sin Selección"

        wCityList = wCountryController.SearchCityByProvinceId(pProvinceId)
        wCityList.Insert(0, wCity)
        pCmbCity.ValueMember = "Id"
        pCmbCity.DisplayMember = "Description"
        pCmbCity.DataSource = wCityList
        pCmbCity.SelectedIndex = 0
    End Sub

    Public Function GetEnumItemDescription(ByVal EnumConstant As [Enum]) As String
        Dim wFi As FieldInfo = EnumConstant.GetType().GetField(EnumConstant.ToString())
        Dim wAttr() As DescriptionAttribute = DirectCast(wFi.GetCustomAttributes(GetType(DescriptionAttribute), False), DescriptionAttribute())
        If wAttr.Length > 0 Then
            Return wAttr(0).Description
        Else
            Return EnumConstant.ToString()
        End If
    End Function

    Public Sub LoadSexListOnCombo(pCmb As ComboBox, ByRef pSexList As List(Of SexBE))
        Dim wSexController As SexController = New SexController
        pSexList = wSexController.Search()
        pCmb.ValueMember = "Id"
        pCmb.DisplayMember = "Description"
        pCmb.DataSource = pSexList
        If pSexList.Count > 0 Then pCmb.SelectedIndex = 0
    End Sub

    Public Sub LoadDocTypeListOnCombo(pCmb As ComboBox, ByRef pDocTypeList As List(Of DocumentTypeBE))
        Dim wDocTypeController As DocumentTypeController = New DocumentTypeController
        pDocTypeList = wDocTypeController.Search()
        pCmb.ValueMember = "Id"
        pCmb.DisplayMember = "Description"
        pCmb.DataSource = pDocTypeList
        If pDocTypeList.Count > 0 Then pCmb.SelectedIndex = 0
    End Sub
End Module
